package lisen.stes.servlet;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class BaseServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	
	protected abstract void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException;
	@SuppressWarnings("unchecked")
	@Override
	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		String _method = request.getParameter("method");
		if (_method == null || _method.trim().equals("")) {
			_method = "execute";
		}
		
		Class clz = this.getClass();
		try {
			Method method = clz.getDeclaredMethod(_method, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			if (e instanceof NoSuchMethodException) {
				response.getWriter().write("404");
				return;
			} else {
				e.printStackTrace();
			}
		}
	}
}